package com.lagou.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.dao.MenuMapper;
import com.lagou.dao.ResourceMapper;
import com.lagou.dao.UserMapper;
import com.lagou.domain.*;
import com.lagou.service.UserService;
import com.lagou.utils.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private ResourceMapper resourceMapper;

    /**
     * 用户分页&条件查询
     *
     * @param userVO
     * @return
     */
    @Override
    public PageInfo<User> findAllUserByPage(UserVO userVO) {

        PageHelper.startPage(userVO.getCurrentPage(), userVO.getPageSize());
        List<User> list = userMapper.findAllUserByPage(userVO);
        return new PageInfo<>(list);
    }

    /**
     * 修改用户状态
     *
     * @param id     用户id
     * @param status 用户状态
     */
    @Override
    public void updateUserStatus(Integer id, String status) {

        userMapper.updateUserStatus(id, status);
    }

    /**
     * 用户登录
     *
     * @param user
     * @return
     */
    @Override
    public User login(User user) throws Exception {

        User login = userMapper.login(user);
        if (login != null && Md5.verify(user.getPassword(), "lagou", login.getPassword())) {

            return login;
        }

        return null;
    }

    /**
     * 分配角色(回显)
     *
     * @param id 用户id
     * @return
     */
    @Override
    public List<Role> findUserRoleById(Integer id) {

        return userMapper.findUserRoleById(id);
    }

    /**
     * 分配角色
     *
     * @param userVO
     */
    @Override
    public void userContextRole(UserVO userVO) {

        userMapper.deleteUserContextRole(userVO.getUserId());
        for (Integer roleId : userVO.getRoleIdList()) {

            User_Role_relation userRoleRelation = new User_Role_relation();
            userRoleRelation.setUserId(userVO.getUserId());
            userRoleRelation.setRoleId(roleId);
            Date date = new Date();
            userRoleRelation.setCreatedBy("system");
            userRoleRelation.setCreatedTime(date);
            userRoleRelation.setUpdatedby("system");
            userRoleRelation.setUpdatedTime(date);
            userMapper.userContextRole(userRoleRelation);
        }
    }

    /**
     * 获取用户权限
     *
     * @param id 用户id
     * @return
     */
    @Override
    public Map<String, Object> getUserPermissions(Integer id) {

        List<Integer> roleIdList = userMapper.findUserRoleById(id).stream().map(Role::getId).collect(Collectors.toList());

        List<Menu> menuList = menuMapper.findParentMenuByRoleId(roleIdList);
        menuList.forEach(menu -> {
            List<Menu> subMenuList = menuMapper.findSubMenuListByPid(menu.getId());
            menu.setSubMenuList(subMenuList);
        });

        List<Resource> resourceList = resourceMapper.findResourceByRoleId(roleIdList);
        Map<String, Object> map = new HashMap<>();
        map.put("menuList", menuList);
        map.put("resourceList", resourceList);
        return map;
    }


}
